using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using AntEater.ComponentModel;

namespace AntEater.Runtime.UI
{
    /// <summary>
    /// Visualizes a Project as a tree node.
    /// </summary>
    internal class TargetExecutionProjectTreeNode : TreeNode
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TargetExecutionProjectTreeNode"/> class.
        /// </summary>
        public TargetExecutionProjectTreeNode()
            : base() {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TargetExecutionProjectTreeNode"/> class.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <param name="options">The options.</param>
        public TargetExecutionProjectTreeNode(Project project, Options options)
            : this() {

            this._project = project;
            this._project.BuildFiles.CollectionChanged += new EventHandler<CollectionChangeEventArgs>(this.BuildFilesCollectionChanged);

            this._options = options;

            // Set node properties
            this.Name = "AntEater";
            this.Text = "AntEater";
            this.ImageKey = "AntEater";
            this.SelectedImageKey = "AntEater";

            this.RefreshNodes();
            this.Expand();
        }

        private Project _project;

        /// <summary>
        /// Gets the project.
        /// </summary>
        /// <value>The project.</value>
        public Project Project {
            get { return _project; }
        }

        private Options _options;

        /// <summary>
        /// Gets the options.
        /// </summary>
        /// <value>The options.</value>
        public Options Options {
            get { return _options; }
        }

        /// <summary>
        /// Refreshes the nodes.
        /// </summary>
        private void RefreshNodes() {

            // Sort build files by name
            SortedList<string, BuildFile> buildFiles = new SortedList<string, BuildFile>(StringComparer.InvariantCultureIgnoreCase);
            foreach (BuildFile buildFile in this.Project.BuildFiles) {
                buildFiles.Add(buildFile.Name, buildFile);
            }

            // Add new nodes
            this.Nodes.Clear();
            foreach (BuildFile buildFile in buildFiles.Values) {
                this.Nodes.Add(new TargetExecutionBuildFileTreeNode(buildFile, this.Options));
            }

            this.Expand();
        }

        /// <summary>
        /// Builds the files collection changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CollectionChangeEventArgs"/> instance containing the event data.</param>
        private void BuildFilesCollectionChanged(object sender, CollectionChangeEventArgs e) {
            RefreshNodes();
        }
    }
}
